//package cn.nfwx.tv.controller;
//
//import android.app.Activity;
//import android.content.Context;
//import android.os.Handler;
//import android.os.Message;
//import android.support.v4.app.Fragment;
//import android.text.InputFilter;
//import android.text.TextUtils;
//import android.text.method.HideReturnsTransformationMethod;
//import android.text.method.PasswordTransformationMethod;
//import android.util.Log;
//import android.view.View;
//import android.widget.Button;
//import android.widget.CheckBox;
//import android.widget.CompoundButton;
//import android.widget.EditText;
//import android.widget.LinearLayout;
//import android.widget.TextView;
//import android.widget.Toast;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.TypeReference;
//import com.google.gson.Gson;
//import com.kaopiz.kprogresshud.KProgressHUD;
//import com.tencent.mm.opensdk.modelmsg.SendAuth;
//import com.tencent.mm.opensdk.openapi.IWXAPI;
//import com.tencent.mm.opensdk.openapi.WXAPIFactory;
//
//import org.greenrobot.eventbus.EventBus;
//import org.json.JSONObject;
//
//import cn.nfwx.tv.R;
//import cn.nfwx.tv.base.BaseFragment;
//import cn.nfwx.tv.base.NFWXApplication;
//import cn.nfwx.tv.callback.ResponseCallback;
//import cn.nfwx.tv.callback.ResponseListener;
//import cn.nfwx.tv.callback.ResponseWithFailListener;
//import cn.nfwx.tv.conts.Constants;
//import cn.nfwx.tv.events.LoginEvents;
//import cn.nfwx.tv.events.ToLoginEvents;
//import cn.nfwx.tv.events.ToMineItemEvent;
//import cn.nfwx.tv.fragment.me.MineFragment;
//import cn.nfwx.tv.helper.AccountHelper;
//import cn.nfwx.tv.helper.DialogFragmentHelper;
//import cn.nfwx.tv.helper.EditNullFilter;
//import cn.nfwx.tv.helper.NetworkHelper;
//import cn.nfwx.tv.helper.ValueCallback;
//import cn.nfwx.tv.helper.WifiHelper;
//import cn.nfwx.tv.manager.UserManager;
//import cn.nfwx.tv.model.Result;
//import cn.nfwx.tv.model.User;
//import cn.nfwx.tv.model.UserModel;
//import cn.nfwx.tv.util.LogUtil;
//import cn.nfwx.tv.util.MD5Util;
//import cn.nfwx.tv.util.NetworkUtil;
//import cn.nfwx.tv.util.SharePrefUtil;
//import cn.nfwx.tv.util.ToastUtil;
//import cn.nfwx.tv.util.ToolUtil;
//import cn.nfwx.tv.vo.ResultErrorVo;
//
///**
// * Created by dell on 2017/11/20.
// */
//
//public class ForgetPasswdViewController implements View.OnClickListener, NetworkHelper.NetworkStateListener {
//    private Activity activity;
//    private Fragment mFragment;
//    private CheckBox forgetpw_cb;
//    private Button btn_forgetpw;
//    private TextView btn_forgetpw_code, login_by_account, register_fast;
//    private EditText ed_forgetpw_phone, ed_forgetpw_code, ed_forgetpw_pw;
//    private KProgressHUD smsProgress, changePwProgress;
//    private int countDown = 60;
//    private final int MSG_COUNT_PW = 2;//找回密码
//    private ChangePwListener changePwListener;
//    private LinearLayout login_wechat_lin;
//    private IWXAPI iwxapi;
//    private View login_wechat_lin_view;
//    private boolean canSendSms = true;
//
//
//    public ForgetPasswdViewController(Activity activity, BaseFragment fragment, View view) {
//        this.activity = activity;
//        mFragment = fragment;
//        initView(view);
//        NetworkHelper.getInstance().addListener(this);
//    }
//
//    public void initView(View view) {
//        login_wechat_lin_view = findView(view, R.id.login_wechat_lin_view);
//        forgetpw_cb = findView(view, R.id.forgetpw_cb);
//        btn_forgetpw_code = findView(view, R.id.btn_forgetpw_code);
//        ed_forgetpw_phone = findView(view, R.id.ed_forgetpw_phone);
//        ed_forgetpw_code = findView(view, R.id.ed_forgetpw_code);
//        ed_forgetpw_pw = findView(view, R.id.ed_forgetpw_pw);
//        btn_forgetpw = findView(view, R.id.btn_forgetpw);
//        login_by_account = findView(view, R.id.login_by_account);
//        register_fast = findView(view, R.id.register_fast);
//        login_wechat_lin = findView(view, R.id.login_wechat_lin);
//        btn_forgetpw_code.setOnClickListener(this);
//        btn_forgetpw.setOnClickListener(this);
//        login_by_account.setOnClickListener(this);
//        register_fast.setOnClickListener(this);
//        ed_forgetpw_phone.setFilters(new InputFilter[]{new EditNullFilter()});
//        ed_forgetpw_code.setFilters(new InputFilter[]{new EditNullFilter()});
//        ed_forgetpw_pw.setFilters(new InputFilter[]{new EditNullFilter()});
//        ToolUtil.toCheckPhone(activity, ed_forgetpw_phone, 1, new ValueCallback<Boolean>() {
//            @Override
//            public void onReceiveValue(Boolean value) {
//                canSendSms = value;
//            }
//        });
//        forgetpw_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//                if (b) {
//                    ed_forgetpw_pw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
//                } else {
//                    ed_forgetpw_pw.setTransformationMethod(PasswordTransformationMethod.getInstance());
//                }
//            }
//        });
//        iwxapi = NFWXApplication.getInstance().getIwxapi();
//        if (NetworkUtil.getNetworkType(activity) == NetworkUtil.WIFI_NETWORK) {
//            WifiHelper.getInstance()._testIfNeedLogon(new ValueCallback<Boolean>() {
//                @Override
//                public void onReceiveValue(Boolean value) {
//                    if (value) {
//                        shandler.sendEmptyMessage(0);
//                    } else {
//                        shandler.sendEmptyMessage(1);
//                    }
//                }
//            }, "forget1");
//        } else {
//            shandler.sendEmptyMessage(0);
//        }
//    }
//
//    /**
//     * 查找控件
//     *
//     * @param rid
//     * @param <T>
//     * @return
//     */
//    public <T extends View> T findView(View view, int rid) {
//        return (T) view.findViewById(rid);
//    }
//
//    @Override
//    public void onClick(View view) {
//        switch (view.getId()) {
//
//            case R.id.btn_forgetpw_code:
//                String smsForgetPwPhone = ed_forgetpw_phone.getText().toString().trim();
//                if (TextUtils.isEmpty(smsForgetPwPhone)) {
//                    Toast.makeText(activity, "手机号码不能为空", Toast.LENGTH_LONG).show();
//                    return;
//                }
//                if (!ToolUtil.isPhoneLegal(smsForgetPwPhone)) {
//                    Toast.makeText(activity, "请输入正确的手机号码 ", Toast.LENGTH_LONG).show();
//                    return;
//                }
//
//                btn_forgetpw_code.setClickable(false);//防止重复点击Button，否则多次点击button：每隔一秒发送消息多次执行
//                countDown = 60;
//                if (AccountHelper.getInstance().getLiveStats() == Constants.STATS_P2P) {
//                    smsProgress = KProgressHUD.create(activity)
//                            .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
//                            .setCancellable(true)
//                            .setAnimationSpeed(2)
//                            .setDimAmount(0.5f);
//                    smsProgress.show();
//                    UserManager.requestSmsCode(activity, smsForgetPwPhone, "reset", new ResponseWithFailListener<JSONObject>() {
//                        @Override
//                        public void onResponse(String tag, final JSONObject response) {
//                            activity.runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    handler.sendEmptyMessage(MSG_COUNT_PW);
//                                    Toast.makeText(activity, "短信已发送", Toast.LENGTH_LONG).show();
//                                }
//                            });
//
//
//                        }
//
//
//                        @Override
//                        public void onError(String tag, Throwable t) {
//                            activity.runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    btn_forgetpw_code.setClickable(true);
//                                    Toast.makeText(activity, "短信验证码发送失败", Toast.LENGTH_LONG).show();
//                                }
//                            });
//
//                        }
//
//                        @Override
//                        public void onFail(String requestTag, final String response) {
//                            activity.runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    try {
//                                        Gson gson = new Gson();
//                                        ResultErrorVo resultErrorVo = gson.fromJson(response, ResultErrorVo.class);
//                                        btn_forgetpw_code.setClickable(true);
//                                        smsProgress.dismiss();
//                                        Toast.makeText(activity, "null".equals(resultErrorVo.getMsg()) ? "短信验证码发送失败" : resultErrorVo.getMsg(), Toast.LENGTH_LONG).show();
//                                    } catch (Exception e) {
//                                        e.printStackTrace();
//                                    }
//
//                                }
//                            });
//                        }
//
//
//                        @Override
//                        public void onFinish() {
//                            smsProgress.dismiss();
//                        }
//                    });
//                } else if (AccountHelper.getInstance().getLiveStats() == Constants.STATS_QPOP) {
//                    if (!canSendSms) {
//                        Toast.makeText(activity, "该号码未注册", Toast.LENGTH_LONG).show();
//                        return;
//                    }
//                    DialogFragmentHelper.showLoadingDialog(mFragment.getChildFragmentManager());
//                    UserManager.requestIjkSmsCode(activity, smsForgetPwPhone, new ResponseCallback<Integer>() {
//                        @Override
//                        public void onSuccess(String requestTag, final Integer response) {
//                            activity.runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    DialogFragmentHelper.closeLoadingDialog(mFragment.getChildFragmentManager());
//                                    btn_forgetpw_code.setClickable(true);
//                                    if (response > 0) {
//                                        handler.sendEmptyMessage(MSG_COUNT_PW);
//                                        Toast.makeText(activity, "短信已发送", Toast.LENGTH_LONG).show();
//                                    }
//                                }
//                            });
//                        }
//
//                        @Override
//                        public void onFailure(String requestTag, String message) {
//                            activity.runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    btn_forgetpw_code.setClickable(true);
//                                    DialogFragmentHelper.closeLoadingDialog(mFragment.getChildFragmentManager());
//                                }
//                            });
//                        }
//                    });
//                }
//                break;
//            case R.id.btn_forgetpw:
//                String smsForgetMobileNo = ed_forgetpw_phone.getText().toString().trim();
//                if (TextUtils.isEmpty(smsForgetMobileNo)) {
//                    Toast.makeText(activity, "请输入手机号码", Toast.LENGTH_LONG).show();
//                    return;
//                }
//                if (!ToolUtil.isPhoneLegal(smsForgetMobileNo)) {
//                    Toast.makeText(activity, "请输⼊正确的⼿机号码", Toast.LENGTH_LONG).show();
//                    return;
//                }
//                String smsForgetPwCode = ed_forgetpw_code.getText().toString().trim();
//                if (TextUtils.isEmpty(smsForgetPwCode)) {
//                    Toast.makeText(activity, "请输入验证码", Toast.LENGTH_LONG).show();
//                    return;
//                }
//
//                if (smsForgetPwCode.length() != 6 || !ToolUtil.isNumeric(smsForgetPwCode)) {
//                    Toast.makeText(activity, "验证码长度为6位 ", Toast.LENGTH_LONG).show();
//                    return;
//                }
//
//                String smsForgetPwNew = ed_forgetpw_pw.getText().toString().trim();
//                if (TextUtils.isEmpty(smsForgetPwNew)) {
//                    Toast.makeText(activity, "请输入密码", Toast.LENGTH_LONG).show();
//                    return;
//                }
//                if (smsForgetPwNew.length() < 6 || smsForgetPwNew.length() > 15) {
//                    Toast.makeText(activity, "密码长度为6-15位", Toast.LENGTH_LONG).show();
//                    return;
//                }
//                if (AccountHelper.getInstance().getLiveStats() == Constants.STATS_P2P) {
//                    changePwProgress = KProgressHUD.create(activity)
//                            .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
////                        .setLabel("加载中")
////                        .setDetailsLabel("Downloading data")
//                            .setCancellable(true)
//                            .setAnimationSpeed(2)
//                            .setDimAmount(0.5f);
//                    changePwProgress.show();
//                    UserManager.changeUserPasswd(activity, smsForgetMobileNo, smsForgetPwCode, smsForgetPwNew, new ResponseWithFailListener<JSONObject>() {
//                        @Override
//                        public void onResponse(String tag, final JSONObject response) {
//                            activity.runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    Log.d("修改密码", response.toString());
//                                    Gson gson = new Gson();
//                                    UserModel user = gson.fromJson(response.toString(), UserModel.class);
//                                    SharePrefUtil.saveUser(activity, user);
//                                    EventBus.getDefault().post(new LoginEvents(user));
//                                    changePwProgress.dismiss();
//                                    Toast.makeText(activity, "密码修改成功", Toast.LENGTH_LONG).show();
//                                    ed_forgetpw_phone.setText("");
//                                    ed_forgetpw_code.setText("");
//                                    ed_forgetpw_pw.setText("");
//                                    if (changePwListener != null) {
//                                        changePwListener.onSucess();
//                                    }
//                                }
//                            });
//
//                        }
//
//
//                        @Override
//                        public void onError(String tag, Throwable t) {
//                            activity.runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    changePwProgress.dismiss();
//                                    Toast.makeText(activity, "密码修改失败", Toast.LENGTH_LONG).show();
//                                }
//                            });
//                        }
//
//                        @Override
//                        public void onFail(String requestTag, final String response) {
//                            activity.runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    Gson gson = new Gson();
//                                    ResultErrorVo resultErrorVo = gson.fromJson(response, ResultErrorVo.class);
//                                    changePwProgress.dismiss();
//                                    Toast.makeText(activity, resultErrorVo.getMsg(), Toast.LENGTH_LONG).show();
//                                }
//                            });
//                        }
//
//                        @Override
//                        public void onFinish() {
//                            changePwProgress.dismiss();
//                        }
//                    });
//                } else if (AccountHelper.getInstance().getLiveStats() == Constants.STATS_QPOP) {
//                    if (!canSendSms) {
//                        Toast.makeText(activity, "该号码未注册", Toast.LENGTH_LONG).show();
//                        return;
//                    }
//                    DialogFragmentHelper.showLoadingDialog(mFragment.getChildFragmentManager());
//                    UserManager.requestIjkResetPwd(activity, smsForgetMobileNo, MD5Util.MD5(smsForgetPwNew), smsForgetPwCode, new ResponseListener<String>() {
//                        @Override
//                        public void onResponse(String requestTag, final String response) {
//                            activity.runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    DialogFragmentHelper.closeLoadingDialog(mFragment.getChildFragmentManager());
//                                    Result<User> result = JSON.parseObject(response, new TypeReference<Result<User>>() {
//                                    }.getType());
//                                    if (Result.RESULT_SUCCESS.equals(result.getMessage())) {
//                                        if (result.getData() != null && result.getData().getUid() > 0) {
//                                            ToastUtil.show(activity, "修改成功");
//                                            AccountHelper.getInstance().saveUser(activity, result.getData());
//
//                                            EventBus.getDefault().post(new ToMineItemEvent(MineFragment.POSITION_SUBSCRIPT));
//                                        } else {
//                                            ToastUtil.show(activity, "修改失败");
//                                        }
//                                    } else {
//                                        ToastUtil.show(activity, result.getMessage());
//                                    }
//                                }
//                            });
//                        }
//
//                        @Override
//                        public void onError(String requestTag, Throwable t) {
//                            activity.runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    DialogFragmentHelper.closeLoadingDialog(mFragment.getChildFragmentManager());
//                                    ToastUtil.show(activity, "修改失败");
//                                }
//                            });
//                        }
//
//                        @Override
//                        public void onFinish() {
//
//                        }
//                    });
//                }
//                break;
//            case R.id.login_by_account:
//                EventBus.getDefault().post(new ToLoginEvents(0));
//                break;
//            case R.id.register_fast:
//                EventBus.getDefault().post(new ToLoginEvents(1));
//                break;
//            case R.id.login_wechat_lin:
//                SendAuth.Req req = new SendAuth.Req();
//                req.scope = "snsapi_userinfo";
//                req.state = "nfwx";
//                iwxapi.sendReq(req);
//                break;
//
//        }
//    }
//
//    public Handler handler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            switch (msg.what) {
//                case MSG_COUNT_PW:
//                    if (countDown >= 0) {
//                        btn_forgetpw_code.setText(countDown-- + "s");
//                        handler.sendEmptyMessageDelayed(MSG_COUNT_PW, 1000);
//                    } else {
//                        btn_forgetpw_code.setClickable(true);
//                        countDown = 60;//复位，60s倒计时完后可以继续倒计时
//                        btn_forgetpw_code.setText("获取验证码");
//                    }
//                    break;
//            }
//
//        }
//    };
//
//
//    public void setChangePwListener(ChangePwListener changePwListener) {
//        this.changePwListener = changePwListener;
//    }
//
//    @Override
//    public void onNetworkChange(int networkType) {
//        if (NetworkUtil.getNetworkType(activity) == NetworkUtil.WIFI_NETWORK) {
//            WifiHelper.getInstance()._testIfNeedLogon(new ValueCallback<Boolean>() {
//                @Override
//                public void onReceiveValue(Boolean value) {
//                    if (value) {
//                        shandler.sendEmptyMessage(0);
//                    } else {
//                        shandler.sendEmptyMessage(1);
//                    }
//                }
//            }, "forget2");
//        } else {
//            shandler.sendEmptyMessage(0);
//        }
//    }
//
//    public interface ChangePwListener {
//        void onSucess();
//
//    }
//
//    Handler shandler = new Handler() {
//        @Override
//        public void dispatchMessage(Message msg) {
//            super.dispatchMessage(msg);
//            switch (msg.what) {
//                case 0:
//                    login_wechat_lin.setVisibility(View.VISIBLE);
//                    login_wechat_lin_view.setVisibility(View.VISIBLE);
//                    break;
//                case 1:
//                    login_wechat_lin.setVisibility(View.INVISIBLE);
//                    login_wechat_lin_view.setVisibility(View.INVISIBLE);
//                    break;
//            }
//
//        }
//    };
//
//    public void onDestroy() {
//        NetworkHelper.getInstance().removeListener(this);
//
//    }
//}
